package priv.cwr.springboot.demo.mapper;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PathVariable;
import priv.cwr.springboot.demo.model.db.UserEntity;
import priv.cwr.springboot.demo.model.vo.UserForm;

import java.util.List;

/**
 * User: R
 * Date: 2018/8/29
 * Time: 11:00
 * Created with IntelliJ IDEA.
 */
@Mapper
@Repository
public interface UserMapper extends BaseMapper<UserEntity> {
    /**
     * 查询用户列表(PC端)
     *
     * @param name  用户名称
     * @param phone 电话
     * @param depts 部门
     * @return
     */
    @SelectProvider(type = UserProvider.class, method = "findAll")
    @Results({
            @Result(column = "user_id", property = "userId"),
            @Result(column = "dept_id", property = "deptId")
    })
    List<UserForm> findAll(@Param("name") String name,
                           @Param("phone") String phone,
                           @Param("depts") String depts);

    /**
     * 查询用户列表（移动端）
     *
     * @param deptId 部门id
     * @return
     */
    @SelectProvider(type = UserProvider.class, method = "findAllByApp")
    @Results({
            @Result(column = "user_id", property = "userId"),
            @Result(column = "dept_id", property = "deptId")
    })
    List<UserForm> findAllByApp(@Param("deptId") Long deptId);

    /**
     * 批量移动用户到指定部门
     *
     * @param deptId 部门id
     * @param ids    用户id集合
     * @return
     */
    @Update("<script>" +
            "UPDATE t_user SET dept_id = #{deptId} WHERE user_id IN " +
            "<foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</script>")
    int move(@Param("deptId") Long deptId,@Param("ids") List<Long> ids);
}
